package com.dormitory.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dormitory.manage.mapper.UserMapper;
import com.dormitory.manage.pojo.User;
import com.dormitory.manage.service.UserService;
import com.dormitory.manage.vo.LoginVo;
import com.dormitory.manage.vo.UpdateInfoVo;
import com.dormitory.manage.vo.UpdatePasswordVo;
import org.springframework.stereotype.Service;

/**
 * @author Administrator
 * @version 1.0
 * @description userServiceImpl
 * @date 2023/4/5 0005 13:40
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    /**
    * @description 用户登录
    * @param loginVo
    * @returnType void
    * @author Administrator
    * @date  13:43
    */
    @Override
    public boolean login(LoginVo loginVo) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",loginVo.getUsername()).eq("password",loginVo.getPassword()).eq("roles",loginVo.getRoles());
        User user = this.baseMapper.selectOne(queryWrapper);
        if (null != user){
            if (user.getIsOnline() == 0){
                user.setAccesses(user.getAccesses()+1);
                user.setIsOnline(1);
                this.baseMapper.updateById(user);
            }else {
                return false;
            }

        }
        return null != user;
    }

    /**
    * @description 根据用户名获取用户
    * @param username
    * @returnType com.demo.pojo.User
    * @author Administrator
    * @date  13:43
    */
    @Override
    public User getUserByUsername(String username) {
        return this.baseMapper.selectOne(new QueryWrapper<User>().eq("username", username));
    }

    @Override
    public User getUserInfoByToken(String token) {
        return this.baseMapper.selectOne(new QueryWrapper<User>().eq("token", token));
    }

    @Override
    public void setToken(String username, String token) {
        User user = this.baseMapper.selectOne(new QueryWrapper<User>().eq("username", username));
        user.setToken(token);
        this.baseMapper.updateById(user);
    }

    @Override
    public boolean updatePassword(UpdatePasswordVo updatePasswordVo) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",updatePasswordVo.getUsername())
                .eq("password",updatePasswordVo.getPassword());
        User user = this.baseMapper.selectOne(queryWrapper);
        if (null != user){
            user.setPassword(updatePasswordVo.getNewPassword());
            return this.baseMapper.updateById(user) > 0;
        }
        return false;
    }

    /**
    * @description 更新用户信息
    * @param updateInfoVo
    * @returnType boolean
    * @author Administrator
    * @date  16:04
    */
    @Override
    public boolean updateInfo(String username,UpdateInfoVo updateInfoVo) {
        User user = this.baseMapper.selectOne(new QueryWrapper<User>().eq("username", username));
        if (null != user){
            user.setUsername(updateInfoVo.getUsername());
            user.setQq(updateInfoVo.getQq());
            user.setEmail(updateInfoVo.getEmail());
            user.setAddress(updateInfoVo.getAddress());
            user.setGender(updateInfoVo.getGender());
            user.setIntroduction(updateInfoVo.getIntroduction());
            user.setPhone(updateInfoVo.getPhone());
            return this.baseMapper.updateById(user) > 0;
        }
        return false;
    }

    @Override
    public boolean logout(String token) {
        if (null == token)  return false;
        User user = this.baseMapper.selectOne(new QueryWrapper<User>().eq("token", token));
        user.setToken(null);
        user.setIsOnline(0);
        return this.baseMapper.updateById(user) > 0;
    }
}
